package org.example.day03;

/**
 * 第二章节2.1.8:同步不具有继承性
 */
public class MyThread12 {
    synchronized public void serviceMethod(){
        try {
            System.out.println("ThreadName = " + Thread.currentThread().getName() + " beginTime = " + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("ThreadName = " + Thread.currentThread().getName() + " endTime = " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SubMyThread12 sub = new SubMyThread12();
        Thread12A ta = new Thread12A(sub);
        ta.setName("A");
        ta.start();
        Thread12B tb = new Thread12B(sub);
        tb.setName("B");
        tb.start();
    }
}

class SubMyThread12 extends MyThread12{
    @Override
    synchronized public void serviceMethod() {
        try {
            System.out.println("ThreadName = " + Thread.currentThread().getName() + " sub beginTime = " + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("ThreadName = " + Thread.currentThread().getName() + " sub endTime = " + System.currentTimeMillis());
            super.serviceMethod();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread12A extends Thread{
    private SubMyThread12 sub;

    public Thread12A(SubMyThread12 sub) {
        this.sub = sub;
    }

    @Override
    public void run() {
        sub.serviceMethod();
    }
}

class Thread12B extends Thread{
    private SubMyThread12 sub;

    public Thread12B(SubMyThread12 sub) {
        this.sub = sub;
    }
    @Override
    public void run() {
        sub.serviceMethod();
    }
}